package com.qing.springcloud.model;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ChatMessage {

    /**
     * 消息UUID
     */
    private String msgId;

    private MsgType msgType;
    /**
     * 消息内容
     */
    private String content;
    /**
     * 发送者ID
     */
    private Long senderId;
    /**
     * 接收者ID(用户或群)
     */
    private Long receiverId;
    /**
     * 聊天类型(0:私聊,1:群聊)
     */
    private Integer chatType;


    private Long timestamp;

    /**
     * 扩展信息(位置坐标/文件信息等)
     */
    private String extra;

    /**
     * 消息UUID
     */
    private List<String> msgIds;

    // 构造函数、getters和setters

    public static ChatMessage fromJson(String json) {
        try {
            return new ObjectMapper().readValue(json, ChatMessage.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Invalid message format", e);
        }
    }

    public String toJson() {
        try {
            return new ObjectMapper().writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to serialize message", e);
        }
    }
}

